﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductAging_ManuAging_AgingAdd : System.Web.UI.Page
{
    DataTable dt;
    Aging ag;
    AgingItem agi;
    AgingInfo aginfo;
    ProductType_AgingInfo pai;
    ProductType_AgingInfoInfo paii;
    //public static string agingID;

    private string agingID
    {
        get { return ViewState["agingID"].ToString(); }
        set { ViewState["agingID"] = value; }
    }

    public ProductAging_ManuAging_AgingAdd()
    {
        dt = new DataTable();
        ag = new Aging();
        agi = new AgingItem();
        pai = new ProductType_AgingInfo();
        paii = new ProductType_AgingInfoInfo();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Button5.Attributes.Add("onclick", "PasteData()");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox2.Text.Trim();
        if (productSN.Length < 10)
        {
            string str1 = "腔体序列号过短，请检查！";
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (productSN.Length > 30)
        {
            string str1 = "腔体序列号过长，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (ProductSNCompare(productSN))
        {
            string str1 = "腔体序列号已在列表中！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            this.ListBox1.Items.Add(productSN);
            this.Label1.Text = this.ListBox1.Items.Count.ToString();
            this.TextBox2.Text = "";
            this.TextBox2.Focus();
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        string str = this.myVal.Value;
        string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string st in strs)
        {
            if (!ProductSNCompare(st))
                this.ListBox1.Items.Add(st.Trim());
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItemCollection lic = new ListItemCollection();
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Selected)
                lic.Add(item);
        }
        foreach (ListItem item in lic)
        {
            this.ListBox1.Items.Remove(item);
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        agingID = this.TextBox1.Text.Trim();
        if (ag.Aging_ExistsID(agingID))
        {
            string str1 = "批次编号已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (this.ListBox1.Items.Count == 0)
        {
            string str1 = "请先添加腔体序列号！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (string.IsNullOrEmpty(this.TextBox5.Text.Trim()))
        {
            string str1 = "请先填写产品类型信息";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            BiuldDT();
            string hasInValidPostStepSN = ag.Aging_HasValidPostStep(this.TextBox5.Text.Trim(), dt);
            if (hasInValidPostStepSN != "Pass")
            {
                string str1 = "不能提交，请反查此腔体的七大管控工序的数据：" + hasInValidPostStepSN;
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
            else if (this.DropDownList1.SelectedItem.Text == "二次时效")
            {
                string secondAgingnotOKSN = ag.Aging_IsSecondAgingOK(dt);
                if (secondAgingnotOKSN != "Pass")
                {
                    string str1 = "此腔体序列号的一次时效没通过，或没有做过一次时效：" + secondAgingnotOKSN;
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                }
                else
                {
                    DataInsert();
                }
            }
            else
            {
                DataInsert();
            }
        }
    }

    //生成DataTable
    protected void BiuldDT()
    {
        dt.Columns.Add("AgingID");
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (ListItem item in this.ListBox1.Items)
        {
            dr = dt.NewRow();
            dr["AgingID"] = agingID;
            dr["ProductSN"] = item.Text;
            dt.Rows.Add(dr);
        }
    }

    //往数据库中插入数据
    protected void DataInsert()
    {
        /*
        string userID = User.Identity.Name;
        string productTypeID = this.TextBox5.Text.Trim();
        int amount = dt.Rows.Count;
        decimal timeSpec = decimal.Parse(this.Label3.Text.Trim());
        int tempSpec = int.Parse(this.Label4.Text.Trim());
        string agingStep = this.DropDownList1.SelectedItem.Text;
        string attributes = this.Label2.Text.Trim();
        string department = this.DropDownList2.SelectedItem.Text;
         */
        aginfo = new AgingInfo(agingID, User.Identity.Name, this.TextBox5.Text.Trim(), dt.Rows.Count, decimal.Parse(this.Label3.Text.Trim()),
            int.Parse(this.Label4.Text.Trim()), this.DropDownList1.SelectedItem.Text, this.Label2.Text.Trim(),
            this.DropDownList2.SelectedItem.Text);               //修改重点在这一句
        if (ag.AgingAndItem_Insert(aginfo, dt))
        //if (agi.AgingItem_Insert(dt) && ag.Aging_Insert(aginfo))
        {
            string str1 = "信息插入成功！这一批共有" + aginfo.Amount + "个腔体。";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            string str1 = "信息插入失败！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        dt.Dispose();
    }

    //调出相应产品类型与时效阶段相关的产品时效信息
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductType_AgingInfo();
    }

    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {
        BindProductType_AgingInfo();
    }

    private void BindProductType_AgingInfo()
    {
        string agingStep = this.DropDownList1.SelectedItem.Text.Trim();
        string productTypeID = this.TextBox5.Text.Trim();
        if (string.IsNullOrEmpty(productTypeID))
        {
            ClearProductType_AgingInfo();
        }
        else
        {
            paii = pai.GetPAI(productTypeID, agingStep);
            if (paii.ProductTypeID == null)
            {
                string str1 = "不存在此时效阶段与产品类型的产品时效信息，请检查！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                ClearProductType_AgingInfo();
            }
            else
            {
                this.Label2.Text = paii.Attributes;
                this.Label3.Text = paii.TimeSpec.ToString();
                this.Label4.Text = paii.TempSpec.ToString();
            }
        }
    }

    private void ClearProductType_AgingInfo()
    {
        this.Label2.Text = "";
        this.Label3.Text = "";
        this.Label4.Text = "";
    }
}
